<template>
  <el-container class="main-container">
    <!-- 头部区域 -->
    <el-header>
      <!-- 左侧的 logo -->
      <img src="../../assets/images/logo.png" alt="" />
      <!-- 右侧的菜单 -->
      <el-menu
        class="el-menu-top"
        mode="horizontal"
        background-color="#23262E"
        text-color="#fff"
        active-text-color="#409EFF"
      >
        <el-submenu index="1">
          <template slot="title">
            <!-- 头像 -->
            <img :src="userInfo.user_pic" alt="" class="avatar" />
            <span>{{ userInfo.username }}</span>
          </template>
          <el-menu-item index="1-1"
            ><i class="el-icon-s-operation"></i>基本资料</el-menu-item
          >
          <el-menu-item index="1-2"
            ><i class="el-icon-camera"></i>更换头像</el-menu-item
          >
          <el-menu-item index="1-3"
            ><i class="el-icon-key"></i>重置密码</el-menu-item
          >
        </el-submenu>
        <el-menu-item index="2" @click="logout"
          ><i class="el-icon-switch-button"></i>退出</el-menu-item
        >
      </el-menu>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <!-- 侧边栏区域
      1. default-active 当前选中的菜单是谁
      2. unique-opened 同时只能打开一个分组的子菜单
      3、说明$route.path  获
       -->
      <el-aside width="200px">
        <el-menu
          default-active="2-1"
          class="el-menu-vertical-demo"
          background-color="#23262E"
          text-color="#fff"
          active-text-color="#409EFF"
          :unique-opened="true"
        >
          <!-- == 使用menus数组动态渲染菜单？== -->
          <!-- v-for应该写到谁的身上？
          写到template上进行变量（template不会生成任何元素）
           -->
          <template v-for="item in menus">
            <!-- 1. 一级没有子 -->
            <el-menu-item
              :index="item.indexPath"
              :key="item.indexPath"
              v-if="!item.children"
              ><i :class="item.icon"></i>>{{ item.title }}</el-menu-item
            >

            <!-- 2. 分组有子 -->
            <el-submenu :key="item.indexPath" v-else :index="item.indexPath">
              <!-- 1. 分组的标题title -->
              <!-- <template slot="title"> -->
              <template #title>
                <i :class="item.icon"></i>
                <span>{{ item.title }}</span>
              </template>
              <!-- 分组下的子菜单=》再次进行v-for -->
              <el-menu-item
                v-for="child in item.children"
                :key="child.indexPath"
                :index="child.indexPath"
                ><i :class="child.icon"></i>{{ child.title }}</el-menu-item
              >
            </el-submenu>
          </template>
        </el-menu>
      </el-aside>
      <el-container>
        <!-- 页面主体区域 -->
        <el-main> Main.vue后台主页 </el-main>
        <!-- 底部 footer 区域 -->
        <el-footer>© www.itheima.com - 黑马程序员</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
// import { delToken } from '@/utils/auth'
import { mapActions, mapState } from 'vuex'
// 导入获取menu数据的实例
import { getMenApi } from '@/api/menu'
export default {
  name: 'Main',
  data () {
    return {
      menus: []
    }
  },
  created () {
    // 发送请求，获取用户信息
    this.getUserAction()
    // 获取菜单数据
    this.getMenu()
  },
  computed: {
    ...mapState('Lgin', ['userInfo'])
  },
  methods: {
    ...mapActions('Lgin', ['getUserAction']),
    // 1、登录
    logout () {
      console.log(this.$confirm)
      this.$confirm('确认退出嘛?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'

      }).then(() => {
        // TODO：执行退出登录的操作
        // 1. 清空 token
        this.$store.commit('Lgin/delToken')
        // 2. 跳转到登录页面
        // this.$router.push('/login')
        this.$router.replace('/login')
      })
        .catch(err => err)
    },
    // 2、获取菜单数据
    async getMenu () {
      const { data } = await getMenApi()
      console.log('menu数据1', data)
      this.menus = data.data
    }

  }
}

</script>

<style lang="less" scoped>
.main-container {
  height: 100%;
  .el-header,
  .el-aside {
    background-color: #23262e;
  }
  // 覆盖菜单样式
  .el-menu-item.is-active,
  .el-menu-item:hover {
    color: #fff !important;
    background: orange !important;
    // 图标颜色
    i {
      color: #fff;
    }
  }
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #f2f2f2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
}
</style>
